package com.project.market.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.project.market.db.domain.MarketIssue;
import com.project.market.db.domain.MarketIssueExample;
import com.project.market.db.mapper.MarketIssueMapper;
import com.project.market.service.AdminIssueService;
import com.project.market.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminIssueServiceImpl implements AdminIssueService {


    /*
    如果question为null，表示没有做查询操作，此时显示出所有通用问题
    如果question不为null，表示此时做查询操作，根据quesion内容做模糊查询，显示出相关信息
     */
    @Override
    public List<MarketIssue> list(Integer page, Integer limit, String question, String sort, String order) {
        // 获取session
        SqlSession session = MybatisUtil.getSession();

        // 获取mapper
        MarketIssueMapper issueMapper = session.getMapper(MarketIssueMapper.class);

        // 获取example
        MarketIssueExample marketIssueExample = new MarketIssueExample();

        // 设置查找条件
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();

        if (question != null) {
            criteria.andQuestionLike("%" + question + "%");
        }

        // 逻辑删除，对于删除的通用问题，deleted设置为true，展示的时候筛出掉deleted为true的通用问题
        criteria.andDeletedEqualTo(false);

        // add_time desc
        marketIssueExample.setOrderByClause(sort + " " + order);

        // 分页
        Page<Object> startPage = PageHelper.startPage(page, limit);

        List<MarketIssue> marketIssues = issueMapper.selectByExample(marketIssueExample);

        // 关闭session
        session.commit();
        session.close();

        return marketIssues;
    }

    /*
    新建、添加通用问题
     */
    @Override
    public MarketIssue create(String question, String answer) {

        // 获取session
        SqlSession session = MybatisUtil.getSession();

        // 获取mapper
        MarketIssueMapper issueMapper = session.getMapper(MarketIssueMapper.class);

        // 根据传入的参数创建marketIssue对象
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);
        marketIssue.setAddTime(LocalDateTime.now());
        marketIssue.setUpdateTime(LocalDateTime.now());
        marketIssue.setDeleted(false);

        // 做插入操作
        issueMapper.insert(marketIssue);

        // 关闭session
        session.commit();
        session.close();

        return marketIssue;
    }

    /*
    更新，编辑通用问题
     */
    @Override
    public MarketIssue update(String addTime, String answer, String id, String question, String updateTime) {

        // 获取session
        SqlSession session = MybatisUtil.getSession();

        // 获取mapper
        MarketIssueMapper issueMapper = session.getMapper(MarketIssueMapper.class);

        // 获取example
        MarketIssueExample marketIssueExample = new MarketIssueExample();

        // 设置修改条件
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();

        criteria.andIdEqualTo(Integer.valueOf(id));

        // 新建marketIssue对象根据传入的参数创建marketIssue对象
        MarketIssue marketIssue = issueMapper.selectByPrimaryKey(Integer.valueOf(id));

/*        // 时间转换格式
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");*/

        marketIssue.setUpdateTime(LocalDateTime.now());
        marketIssue.setAnswer(answer);
        marketIssue.setQuestion(question);

        // 执行update操作
        issueMapper.updateByExample(marketIssue, marketIssueExample);

        // 关闭session
        session.commit();
        session.close();
        return marketIssue;
    }


    /*
    删除操作，根据id删除，把deleted列的内容改为 true
    逻辑删除，对于删除的通用问题，deleted设置为true，展示的时候筛出掉deleted为true的通用问题
     */
    @Override
    public MarketIssue delete(String addTime, String answer, Boolean deleted, Integer id, String question, String updateTime) {

        // 获取session
        SqlSession session = MybatisUtil.getSession();

        // 获取mapper
        MarketIssueMapper issueMapper = session.getMapper(MarketIssueMapper.class);

        // 获取example
        MarketIssueExample marketIssueExample = new MarketIssueExample();

        // 设置修改条件
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();

        criteria.andIdEqualTo(Integer.valueOf(id));

        MarketIssue marketIssue = issueMapper.selectByPrimaryKey(id);
        marketIssue.setDeleted(true);
        // 执行update操作
        issueMapper.updateByExample(marketIssue, marketIssueExample);

        // 关闭session
        session.commit();
        session.close();

        return marketIssue;
    }


}